<?php

class AvisoController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','viewcelular','viewaccesorio','viewserviciot'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('cargarcelular','cargaraccesorio','cargarserviciot','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionViewserviciot($id)
	{
		$this->render('viewserviciot',array(
			'model'=>$this->loadModel($id),
		));
	}
	public function actionViewcelular($id)
	{
		$this->render('viewcelular',array(
			'model'=>$this->loadModel($id),
		));
	}
	public function actionViewaccesorio($id)
	{
		$this->render('viewaccesorio',array(
			'model'=>$this->loadModel($id),
		));
	}

	public function actioncargarcelular()
	{
		$model=new Aviso;
		$a=new Celular;
		$b=new Modelo;
		$this->performAjaxValidation(array($model,$a,$b));
		if(isset($_POST['Aviso'],$_POST['Celular'],$_POST['Modelo'])){
			$model->attributes=$_POST['Aviso'];
			$a->attributes=$_POST['Celular'];
			$b->attributes=$_POST['Modelo'];
			$valid=$b->validate();
			if($valid)
                        if($b->save(false)){
							$a->idModelo=$b->id;
							if(!$a->camara)
								$a->mp='0';
                            if($a->save()){
                                    $model->idUsuario=Yii::app()->user->id; 
                                    $model->fechaAlta=date('d/m/Y h:m:s');
                                    $model->idArticulo='c'.$a->id;
                                    if($model->save())
                                            $this->redirect(array('viewcelular','id'=>$model->id));	
                            }
                        }
		}
		$this->render('cargarcelular',array('model'=>$model,'a'=>$a,'b'=>$b));
	}
        
	public function actioncargaraccesorio()
	{
		$model=new Aviso;
		$a=new Accesorio;
		$b=new Modelo;
		$this->performAjaxValidation(array($model,$a,$b));
                if(isset($_POST['Aviso'])&& isset($_POST['Accesorio']) && isset($_POST['Modelo'])){
			$model->attributes=$_POST['Aviso'];
			$a->attributes=$_POST['Accesorio'];
			$b->attributes=$_POST['Modelo'];
			$valid=$b->validate();
			if($valid)
                        if($b->save(false)){
                                    $a->idModelo=$b->id;
                            if($a->save()){
                                    $model->idUsuario=Yii::app()->user->id;
                                    $model->fechaAlta=date('d/m/Y h:m:s');
                                    $model->idArticulo='a'.$a->id;
                                    if($model->save())
                                            $this->redirect(array('viewaccesorio','id'=>$model->id));	
                            }
                        }
		}
		$this->render('cargaraccesorio',array('model'=>$model,'a'=>$a,'b'=>$b));
	}
        
        public function actioncargarserviciot()
	{
		$model=new Aviso;
		$a=new Serviciotecnico;
		$this->performAjaxValidation(array($model,$a));
		if(isset($_POST['Aviso'],$_POST['Serviciotecnico']))
		{
			$model->attributes=$_POST['Aviso'];
			$a->attributes=$_POST['Serviciotecnico'];
			$valid=$a->validate();
			if($valid)
                        if($a->save(false)){
                                $model->idUsuario=Yii::app()->user->id;
                                $model->fechaAlta=date('d/m/Y h:m:s');
                                $model->idArticulo='s'.$a->id;
                                if($model->save())
                                        $this->redirect(array('viewserviciot','id'=>$model->id));	
                        }
		}
		$this->render('cargarserviciot',array('model'=>$model,'a'=>$a));
	}

	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// $this->performAjaxValidation($model);

		if(isset($_POST['Aviso']))
		{
			$model->attributes=$_POST['Aviso'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Aviso');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Aviso('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Aviso']))
			$model->attributes=$_GET['Aviso'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Aviso::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='aviso-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
